package com.vincent.template.controller.biz;

import com.vincent.template.dto.biz.DepartmentDto;
import com.vincent.template.dto.biz.DepartmentTreeDto;
import com.vincent.template.service.logic.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Vincent Wang
 * @Date 2020/10/19 14:37
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping(value = "/departments")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("创建部门")
    @PostMapping(value = "/create")
    public Long addDepartment(@Validated @RequestBody DepartmentDto dto) {
        return departmentService.insert(dto);
    }

    @ApiOperation("更新部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentNo", value = "部门编号", required = true)
    })
    @PutMapping(value = "/{departmentNo}/update")
    public Boolean updateDepartment(@PathVariable(value = "departmentNo") Long departmentNo,
                                    @Validated @RequestBody DepartmentDto dto) {
        return departmentService.update(departmentNo, dto);
    }

    @ApiOperation("移动部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentNo", value = "部门编号", required = true),
            @ApiImplicitParam(name = "parentNo", value = "新的父级部门编号", required = true)
    })
    @PutMapping(value = "/move/{departmentNo}/into/{parentNo}")
    public Boolean moveDepartment(@PathVariable(value = "departmentNo") Long departmentNo,
                                  @PathVariable(value = "parentNo") Long parentNo) {
        return departmentService.move(departmentNo, parentNo);
    }

    @ApiOperation("移除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentNo", value = "部门编号", required = true),
            @ApiImplicitParam(name = "removeSubs", value = "是否同时删除子部门")
    })
    @DeleteMapping(value = "/{departmentNo}/delete")
    public Boolean removeDepartment(@PathVariable(value = "departmentNo") Long departmentNo,
                                    @RequestParam(value = "removeSubs", defaultValue = "false") Boolean removeSubs) {
        return departmentService.delete(departmentNo, removeSubs);
    }

    @ApiOperation("查询单个部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentNo", value = "部门编号", required = true)
    })
    @GetMapping(value = "/{departmentNo}")
    public DepartmentDto findDepartment(@PathVariable(value = "departmentNo") Long departmentNo) {
        return departmentService.findDepartment(departmentNo);
    }

    @ApiOperation("查询所有部门")
    @GetMapping
    public List<DepartmentDto> findAll() {
        return departmentService.findAll();
    }

    @ApiOperation("查询部门树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentNo", value = "部门编号", required = true)
    })
    @GetMapping(value = "/{departmentNo}/tree")
    public DepartmentTreeDto findDepartmentTree(@PathVariable(value = "departmentNo") Long departmentNo) {
        return departmentService.findDepartmentTree(departmentNo);
    }

    @ApiOperation("同步部门全路径名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentNo", value = "部门编号")
    })
    @PutMapping(value = "/syncDepartmentFullName")
    public Boolean syncDepartmentFullName(@RequestParam(value = "departmentNo", defaultValue = "0") Long departmentNo) {
        return departmentService.syncDepartmentFullName(departmentNo);
    }

}
